/**
 * Created by Flagship on 2017/5/9.
 */
define(['angular'], function (angular) {

    /*-----------------------------------------
     *   ����ʼǷ���
     ------------------------------------------*/
    angular.module('books.service.note', [])
        //.config(function ($httpProvider) {
        //    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        //    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        //    $httpProvider.defaults.transformRequest = [function (data) {
        //        var param = function (obj) {
        //            var query = '';
        //            var name, value, fullSubName, subName, subValue, innerObj, i;
        //            for (name in obj) {
        //                value = obj[name];
        //                if (value instanceof Array) {
        //                    for (i = 0; i < value.length; ++i) {
        //                        subValue = value[i];
        //                        fullSubName = name + '[]';
        //                        innerObj = {};
        //                        innerObj[fullSubName] = subValue;
        //                        query += param(innerObj) + '&';
        //                    }
        //                } else if (value instanceof Object) {
        //                    for (subName in value) {
        //                        subValue = value[subName];
        //                        fullSubName = subName;
        //                        innerObj = {};
        //                        innerObj[fullSubName] = subValue;
        //                        query += param(innerObj) + '&';
        //                    }
        //                } else if (value !== undefined && value !== null) {
        //                    query += encodeURIComponent(name) + '='
        //                        + encodeURIComponent(value) + '&';
        //                }
        //            }
        //            return query.length ? query.substr(0, query.length - 1) : query;
        //        };
        //        return angular.isObject(data) && String(data) !== '[object File]'
        //            ? param(data)
        //            : data;
        //    }]})
        .constant('noteServiceConfig', {
            urls:{
                getNodeList:'http://47.92.95.246:8012/listnote',//���ú�̨api�ӿ�
                deleteNode:'http://47.92.95.246:8012/deleteNote',//���ú�̨api�ӿ�
                createNode:'http://47.92.95.246:8012/insertNote',
                updateNode:'http://47.92.95.246:8012/updateNote',
                getNodeDetail:'http://47.92.95.246:8012/getNoteDetail',
                getbookAll:'http://47.92.95.246:8012/book/info/getall'
            }
        })
        .factory('nodeService',['$http', '$q', 'noteServiceConfig', function ($http, $q, noteServiceConfig) {
            return {
                //��ȡ�Ķ��ʼ��б�
                getNodeList: function(page,size,bookName,author){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'GET',
                        dataType: "json",
                        contentType:'application/json; charset=UTF-8',
                        url:noteServiceConfig.urls.getNodeList,
                        params: {
                            page: page,
                            size:size,
                            bookName:bookName,
                            author:author
                        }
                    })
                        .success(function(response){
                            deffered.resolve(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                //ɾ���Ķ��ʼ�
                deleteNode: function(bookId){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'POST',
                        data:{
                            bookId: bookId
                        },
                        url:noteServiceConfig.urls.deleteNode,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function(obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function(response){
                            deffered.resolve(response);
                            console.log(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                //����Ķ��ʼ�
                createNode: function(model){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'POST',
                        //dataType: "json",
                        //contentType:'application/json; charset=UTF-8',
                        url:noteServiceConfig.urls.createNode,
                        data: model,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function(obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function(response){
                            deffered.resolve(response);
                            console.log(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                //�����Ķ��ʼ�
                updateNode: function(model){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'POST',
                        url:noteServiceConfig.urls.updateNode,
                        data: model,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function(obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function(response){
                            deffered.resolve(response);
                            console.log(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                //��ȡ�Ķ��ʼ���ϸ
                getNodeDetail: function(id){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'GET',
                        dataType: "json",
                        contentType:'application/json; charset=UTF-8',
                        url:noteServiceConfig.urls.getNodeDetail,
                        params: {
                            id:id
                        }
                    })
                        .success(function(response){
                            deffered.resolve(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                getbookAll: function(){
                    //���÷��� ͬ����������
                    var deffered = $q.defer();
                    //��������
                    $http({
                        method:'GET',
                        dataType: "json",
                        contentType:'application/json; charset=UTF-8',
                        url:noteServiceConfig.urls.getbookAll
                    })
                        .success(function(response){
                            deffered.resolve(response);
                        })
                        .error(function(e, code){
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
            };
        }]);

});

